﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
namespace DAL
{
    /// <summary>
    /// Summary description for Category
    /// </summary>
    public partial class Category
    {
        public void Update()
        {
            using (DAL.EShopDBDataContext db = new DAL.EShopDBDataContext())
            {
                var dbCategory = db.Categories.Single(cat => cat.CategoryId == this.CategoryId);
                if (this.Parent != null)
                    dbCategory.Parent = this.Parent;
                if (this.Roles != null)
                    dbCategory.Roles = this.Roles;
                if (this.Title != null)
                    dbCategory.Title = this.Title;
                if (this.Url != null)
                    dbCategory.Url = this.Url;
                if (this.Description != null)
                    dbCategory.Description = this.Description;
                db.SubmitChanges();
            }
        }
        public void Update(int? categoryId, int? parent, string roles, string title, string description)
        {
            using (DAL.EShopDBDataContext db = new DAL.EShopDBDataContext())
            {
                var dbCategory = db.Categories.Single(cat => cat.CategoryId == categoryId);
                if (parent != null)
                    dbCategory.Parent = parent;
                if (roles != null)
                    dbCategory.Roles = roles;
                if (title != null)
                    dbCategory.Title = title;
                if (description != null)
                    dbCategory.Description = description;
                db.SubmitChanges();
            }
        }
        public void Delete(int categoryId)
        {
            using (DAL.EShopDBDataContext db = new EShopDBDataContext())
            {
                var subCategoriesCount = (from c in db.Categories
                                          where c.Parent == categoryId
                                          select c).Count();
                if (subCategoriesCount > 0)
                    throw new DeleteImpossibleException(HttpContext.GetGlobalResourceObject("ExceptionMessages", "CategoryWithSubCategoriesDelete").ToString());
                db.Category_Delete(categoryId);
                db.SubmitChanges();
            }
        }

        public void Insert(int parent, string title, string urlId)
        {
            using (DAL.EShopDBDataContext db = new EShopDBDataContext())
            {
                DAL.Category cat = new Category { Title = title, Parent = parent, UrlId = urlId };
                db.Categories.InsertOnSubmit(cat);
                db.SubmitChanges();
            }
        }

        public Category[] SelectByParent(int parentId)
        {
            using (DAL.EShopDBDataContext db = new EShopDBDataContext())
            {
                var categories = from c in db.Categories
                                 where c.Parent == parentId
                                 select c;
                return categories.ToArray();
            }
        }
    }
}
